package com.basic.modules.ykf.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.basic.common.exception.RRException;
import com.basic.common.utils.ParamsModel;
import com.basic.common.utils.ParamUtils;
import com.basic.modules.sys.entity.SysUserEntity;
import com.basic.modules.sys.model.CurSerExcel;
import com.basic.modules.sys.service.SysUserService;
import com.basic.modules.ykf.model.CusSerScoreExcel;
import com.basic.modules.ykf.model.ExcelReq;
import com.basic.modules.ykf.model.YkfCusSerScoreModel;
import com.basic.modules.ykf.utils.ExcelCellWidthStyleStrategy;
import com.basic.modules.ykf.utils.ExcelStyleUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.basic.common.utils.PageUtils;
import com.basic.common.utils.Query;

import com.basic.modules.ykf.dao.YkfCusSerScoreDao;
import com.basic.modules.ykf.entity.YkfCusSerScoreEntity;
import com.basic.modules.ykf.service.YkfCusSerScoreService;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;


@Service("ykfCusSerScoreService")
public class YkfCusSerScoreServiceImpl extends ServiceImpl<YkfCusSerScoreDao, YkfCusSerScoreEntity> implements YkfCusSerScoreService {

    @Autowired
    private SysUserService userService;

    @Override
    public PageUtils queryPage(ParamsModel<YkfCusSerScoreModel> params) {
        if (ObjectUtil.isEmpty(params) || ObjectUtil.isEmpty(params.getPage())) {
            throw new RRException("参数缺失");
        }
        if (ObjectUtil.isEmpty(params.getCondition())) {
            params.setCondition(new YkfCusSerScoreModel());
        }
//        if (ObjectUtil.isNotEmpty(params.getSort()) && StrUtil.isNotBlank(params.getSort().getSidx())) {
//            params.getSort().setSidx(StrUtil.toUnderlineCase(params.getSort().getSidx()));
//        }
        IPage<YkfCusSerScoreEntity> page = new Query<YkfCusSerScoreEntity>().getPage(BeanUtil.beanToMap(params.getPage()));

//        List<Long> powerUserIds = userService.getPowerUserIds();
//        params.getCondition().setCreateUserIds(powerUserIds);

        List<Long> powerUIds = userService.getPowerUIds();
        if (ObjectUtil.isEmpty(powerUIds)) {
            return new PageUtils();
        } else {
            if (!powerUIds.contains(0L)) {
                params.getCondition().setPowerUIds(powerUIds);
            }
        }

        Page<YkfCusSerScoreModel> resultPage = baseMapper.queryPage(page, params.getSort(), params.getCondition());
        return new PageUtils(resultPage);
    }

    @Override
    public void customerServiceExamScores(ExcelReq req, HttpServletResponse response) throws IOException {
        List<CusSerScoreExcel> list = new ArrayList<>();
        //查询列表
        ParamsModel<YkfCusSerScoreModel> params = new ParamsModel<>();
        params.setCondition(req.getCondition(new TypeReference<YkfCusSerScoreModel>() {}));
        params.setSort(req.getSort());
        ParamsModel.PageParams pageParams = new ParamsModel.PageParams();
        pageParams.setIndex(1L);
        pageParams.setSize(9999L);
        params.setPage(pageParams);
        PageUtils pageUtils = queryPage(params);
        List<YkfCusSerScoreModel> dataList = (List<YkfCusSerScoreModel>) pageUtils.getList();
        if (ObjectUtil.isNotEmpty(dataList)) {
            for (YkfCusSerScoreModel record : dataList) {
                CusSerScoreExcel excel = new CusSerScoreExcel();
                BeanUtil.copyProperties(record, excel);
                list.add(excel);
            }
        }
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("客服考试分数", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        HorizontalCellStyleStrategy horizontalCellStyleStrategy =
                new HorizontalCellStyleStrategy(ExcelStyleUtils.getHeadStyle(), ExcelStyleUtils.getContentStyle());
        ExcelCellWidthStyleStrategy widthStyleStrategy = new ExcelCellWidthStyleStrategy();
        EasyExcel.write(response.getOutputStream(), CusSerScoreExcel.class)
                .sheet("客服考试分数")
                .registerWriteHandler(horizontalCellStyleStrategy)
                .registerWriteHandler(widthStyleStrategy)
                .doWrite(list);
    }

}
